package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Department dept) {
        departmentMapper.insert(dept);
    }

    @Override
    public void update(Department dept) {
        departmentMapper.updateByPrimaryKey(dept);
    }

    @Override
    public Department select(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    //调用Mapper接口的实现类对象的方法查询数据，封装成PageResult对象返回
//    @Override
//    public PageResult<Department> query(QueryObject qo) {
//        int count = departmentMapper.selectForCount(qo);
//        if (count == 0) {
//            return new PageResult<>(Collections.emptyList(), qo.getCurrentPage(), qo.getPageSize(), count);
//        }
//        List<Department> departments = departmentMapper.selectForList(qo);
//        return new PageResult<>(departments, qo.getCurrentPage(), qo.getPageSize(), count);
//    }
    @Override
    public PageInfo<Department> query(QueryObject qo) {
        //使用分页插件,传入当前页,每页显示数量
        //告诉pagehelper我们的分页参数（只对下一句sql进行自动分页）
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> departments = departmentMapper.selectForList(qo);
        return new PageInfo(departments);
    }


}
